package com.koron.css2.projectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.projectCenter.bean.query.PjReformQueryBean;
import com.koron.css2.projectCenter.mapper.PjReformMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.JdbcConnection;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工改系统
 *
 * @author 吴建宏 2021年12月8日
 */
public class PjReformList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try{
		//将入参转换为QueryBean
		PjReformQueryBean pjReformQueryBean = JsonUtils.objectToPojo(req.getData(), PjReformQueryBean.class);

		//获取Mapper实例
		PjReformMapper mapper = factory.getMapper(PjReformMapper.class);
		//分页
		PageHelper.startPage(pjReformQueryBean.getPage(), pjReformQueryBean.getPageCount());

		//执行SQL
//		List<PjReformListVO> list = mapper.list(pjReformQueryBean);
			JdbcConnection jc = new JdbcConnection("com.mysql.cj.jdbc.Driver",
				"jdbc:mysql://172.31.116.237:3306/nx_ztsw?useUnicode=true&AutoReconnect=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
				"nxztsw","Epoint@123456");
//			JdbcConnection jc = new JdbcConnection("com.mysql.cj.jdbc.Driver",
//				"jdbc:mysql://36.103.227.86:3306/nx_ztsw?useUnicode=true&AutoReconnect=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
//				"root", "Gdhk@3438");


			jc.createConnection();
			String sql = "SELECT `ITEMNAME` as 'XmName',DATE_FORMAT(`ITEMSTARTDATE`, '%Y-%m-%d') as 'NkgTime',`CONSTRUCTIONSITE` as 'JsPlace',`CONSTRUCTIONPROPERTY` as 'JsEssence', `ITEMLEGALDEPT` as 'XmUnit',`CONSTRUCTIONSCALEANDDESC` as 'JsDetails',`ITEMLEGALCREDITCODE` as 'UscCode', `CONTRACTPERSON` as ' LxPeople',`CONTRACTPHONE` as 'LxPhone',`HANDLEUSERNAME` as 'ClPeople',DATE_FORMAT(`VERSIONTIME`, '%Y-%m-%d') as 'CjTime',DATE_FORMAT(`FINISHTIME`, '%Y-%m-%d') as 'ClTime' FROM `nx_inf_item` a LEFT JOIN `nx_inf_apply_result` b on a.`ROWGUID` = b.`ROWGUID` where 1=1";
			int count = 0;
			String[] param = new String[10];
			if (StringUtils.isNotBlank(pjReformQueryBean.getXmName())){
				sql = sql + " and ITEMSTARTDATE >= ? and ITEMSTARTDATE <= ?  and ITEMNAME like concat('%',?,'%') ";
				param[count] = pjReformQueryBean.getBeginDate();
				count ++;
				param[count] = pjReformQueryBean.getEndDate();
				count ++;
				param[count] = pjReformQueryBean.getXmName();
				count ++;
			}
			if (StringUtils.isNotBlank(pjReformQueryBean.getBeginDate()) && StringUtils.isNotBlank(pjReformQueryBean.getEndDate())){
				sql = sql + " and ITEMSTARTDATE >= ? and ITEMSTARTDATE <= ? ";
				param[count] = pjReformQueryBean.getBeginDate();
				count ++;
				param[count] = pjReformQueryBean.getEndDate();
				count ++;
			}
				List<PjReformQueryBean> list = jc.executeList(sql, PjReformQueryBean.class,param, count);
				System.out.println("----->> list.size() = " + list.size());
				if (null != list && list.size() > 0) {
//                System.out.println("---   》第一条返回结果：XmName == "+ list.get(0));
					for (int i = 0; i < list.size(); i++) {
						System.out.println("---   》返回所有结果：ItemName == " + list.get(i));
					}

				}
				jc.closeDBConnection();
//            System.out.println(userInfoList.get(0).getUserNo());
//				System.out.println(list.size());
		//输出结果
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
		info.setData(new PageInfo(list));
		return info;
	} catch (Exception e) {
		e.printStackTrace();
		logger.error("工改系统列表查询失败"+e.getMessage(), e);
		return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
	}
	}
}
